package com.lemo.fj.config;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter{
	
	/**
	 * fastjson 返回时的情况
	 *  QuoteFieldNames----------输出key时是否使用双引号,默认为true
	 *  UseSingleQuotes, //使用单引号而不是双引号序列化
    	WriteMapNullValue--------是否输出值为null的字段,默认为false
	    WriteNullNumberAsZero----数值字段如果为null,输出为0,而非null
	    WriteNullListAsEmpty-----list字段如果为null，输出为[]，而不是null,默认不输出null
	    WriteNullStringAsEmpty---字符类型字段如果为null,输出为"",而非null
	    WriteNullBooleanAsFalse--Boolean字段如果为null,输出为false,而非null
	    SkipTransientField, //忽略@Transient注解过的字段
    	SortField //按字段名称排序后输出
	    
	 */
	private static SerializerFeature [] arrs ={
			SerializerFeature.WriteMapNullValue,
			SerializerFeature.WriteNullNumberAsZero,
			SerializerFeature.WriteNullStringAsEmpty,
			SerializerFeature.WriteNullBooleanAsFalse,
			SerializerFeature.SortField,
			SerializerFeature.PrettyFormat
	} ;
	
	/**
	 * 转换fastjson
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(arrs);
		fastJsonConfig.setDateFormat("yyyy-MM-dd");
		fastConverter.setFastJsonConfig(fastJsonConfig);
		converters.add(fastConverter);
	}

}
